package com.qc31.monitorlib.uitls

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.annotation.DrawableRes
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.*

object MapUtil {
    //MarkerOptions
    private val mMarkerOptions = MarkerOptions()

    //导航回调
//    val mNaviInfoCallback = SimpleINaviInfoCallback()

    private const val LATLNG_LAT = 0.0018
    private const val LATLNG_LNG = 0.0001
    val latLng = LatLng(23.973850, 120.979685)

    /** 添加Marker */
    fun addMarker(aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int) {
        aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
        )
    }

    /** 添加并获得Marker */
    fun addGetMarker(aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
                .anchor(0.5f, 0.5f)
        )
    }

    /** 添加并获得Marker */
    fun addAndGetMarker(aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
                .anchor(0.5f, 0.8f)
        )
    }


    /** 添加并获得Marker */
    fun addAndGetMarker(
        aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int, rotate: Float
    ): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
                .anchor(0.5f, 0.5f)
                .rotateAngle(rotate).belowMaskLayer(true)
        )
    }

    /** 添加并获得Marker */
    fun addAndGetMarker(
        aMap: AMap, latLng: LatLng, descriptor: BitmapDescriptor, rotate: Float
    ): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(descriptor).anchor(0.5f, 0.5f)
                .rotateAngle(rotate).belowMaskLayer(true)
        )
    }

    /** 添加并获得Marker */
    fun addAndGetMarker(
        aMap: AMap, latLng: LatLng, descriptor: BitmapDescriptor
    ): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(descriptor).anchor(0.12f, 0.5f)
                .belowMaskLayer(true)
        )
    }

    /** 设置地图显示的中心的LatLng */
    fun showMapLatLng(aMap: AMap, latLng: LatLng) {
        aMap.animateCamera(CameraUpdateFactory.newLatLng(latLng))
    }

    /** 设置地图中心LatLng以及缩放级别 默认17 */
    private fun showMapLatLngZoom(aMap: AMap, latLng: LatLng, zoom: Float = 17f) {
        aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom))
    }

    /** 根据点计算可视范围内的地图级别 边框距离默认200 */
    fun showMapBounds(aMap: AMap, latLngBounds: LatLngBounds?, padding: Int = 200) {
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, padding))
    }

    /** 设置弹窗显示位置+偏移默认 LATLNG_LAT LATLNG_LNG */
    fun showLatLngZoom(
        aMap: AMap,
        latLng: LatLng,
        lat: Double = LATLNG_LAT,
        lng: Double = LATLNG_LNG
    ) {
        showMapLatLngZoom(
            aMap,
            LatLng(latLng.latitude - lat, latLng.longitude + lng),
            17f
        )
    }


    /** 设置弹窗显示位置+偏移默认 LATLNG_LAT LATLNG_LNG */
    fun showMapPopupLatLng(
        aMap: AMap,
        latLng: LatLng,
        lat: Double = LATLNG_LAT,
        lng: Double = LATLNG_LNG
    ) {
        showMapLatLngZoom(aMap, LatLng(latLng.latitude + lat, latLng.longitude + lng))
    }

    fun initMapSetting(mAMap: AMap, isZoomBtn: Boolean = false) {
        mAMap.uiSettings.run {
//            //关闭一切手势操作
//            setAllGesturesEnabled(false)

            //缩放按钮
            isZoomControlsEnabled = isZoomBtn
            //旋转功能
            isRotateGesturesEnabled = false
            //双指倾斜手势
            isTiltGesturesEnabled = false
            //定位按钮
            isMyLocationButtonEnabled = false
            //开启比例尺
            isScaleControlsEnabled = true
        }
    }

    /** 设置定位相关信息 默认定位1次 不展示定位蓝点 */
    fun setLocationStyle(aMap: AMap, showLocation: Boolean = false) {
        MyLocationStyle().apply {
            myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW)
            showMyLocation(showLocation)
            aMap.isMyLocationEnabled = true
        }.also { aMap.myLocationStyle = it }

    }

    /** 跳转到拨号界面，同时传递电话号码 */
    fun callPhone(context: Context, phone: String) {
        val dialIntent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phone"))
        context.startActivity(dialIntent)
    }

    /** 开启导航 */
    fun startNavigation(poiName: String, startLatLng: LatLng, endLatLng: LatLng, context: Context) {
//        val params = AmapNaviParams(
//            Poi(poiName, startLatLng, ""), null, Poi("", endLatLng, ""),
//            AmapNaviType.DRIVER, AmapPageType.NAVI
//        ) /*.setTheme(AmapNaviTheme.BLUE)*/
//        params.setUseInnerVoice(true)
//        AmapNaviPage.getInstance().showRouteActivity(context, params, mNaviInfoCallback)

    }
}
